/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.logiclander.ricemine.services.identity;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.dto.KimEntityPrivacyPreferencesInfo;

import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;

/**
 *
 * @author tcarroll
 */
public class LdapKimEntityPrivacyPreferencesInfoMapper implements ContextMapper {

  private String addressSuppressionAttributeName = null;
  private String emailSuppressionAttributeName = null;
  private String nameSuppressionAttributeName = null;
  private String personalSuppressionAttributeName = null;
  private String phoneSuppressionAttributeName = null;
  private final Log log = LogFactory.getLog(getClass());

//beg: methods required by ContextMapper interface
  public Object mapFromContext(Object ctx) {
    validateConfiguration();
    KimEntityPrivacyPreferencesInfo keppi = new KimEntityPrivacyPreferencesInfo();
    try {
      DirContextAdapter context = (DirContextAdapter) ctx;

      String addressSuppressionAttributeValue = context.getStringAttribute(addressSuppressionAttributeName);
      if (addressSuppressionAttributeValue != null && addressSuppressionAttributeValue.equalsIgnoreCase("TRUE")) {
        keppi.setSuppressAddress(true);
      } else {
        keppi.setSuppressAddress(false);
      }
      String emailSuppressionAttributeValue = context.getStringAttribute(emailSuppressionAttributeName);
      if (emailSuppressionAttributeValue != null && emailSuppressionAttributeValue.equalsIgnoreCase("TRUE")) {
        keppi.setSuppressEmail(true);
      } else {
        keppi.setSuppressEmail(false);
      }
      String nameSuppressionAttributeValue = context.getStringAttribute(nameSuppressionAttributeName);
      if (nameSuppressionAttributeValue != null && nameSuppressionAttributeValue.equalsIgnoreCase("TRUE")) {
        keppi.setSuppressName(true);
      } else {
        keppi.setSuppressName(false);
      }
      String personalSuppressionAttributeValue = context.getStringAttribute(personalSuppressionAttributeName);
      if (personalSuppressionAttributeValue != null && personalSuppressionAttributeValue.equalsIgnoreCase("TRUE")) {
        keppi.setSuppressPersonal(true);
      } else {
        keppi.setSuppressPersonal(false);
      }
      String phoneSuppressionAttributeValue = context.getStringAttribute(phoneSuppressionAttributeName);
      if (phoneSuppressionAttributeValue != null && phoneSuppressionAttributeValue.equalsIgnoreCase("TRUE")) {
        keppi.setSuppressPhone(true);
      } else {
        keppi.setSuppressPhone(false);
      }

    } catch (Throwable t) {
      log.error("Error in KimPrincipalContextMapper", t);
      String msg = "KimEntityPrivacyPreferencesContextMapper failed to create a LdapRecord from the specified Context: " + ctx;
      throw new RuntimeException(msg, t);
    }
    if (log.isDebugEnabled()) {
      log.debug("mapped KimEntityPrivacyPreferences: suppressAddress=" + keppi.isSuppressAddress());
    }
    return keppi;
  }

//end: methods required by ContextMapper interface
//beg: helper methods
  public String getAddressSuppressionAttributeName() {
    return this.addressSuppressionAttributeName;
  }

  public void setAddressSuppressionAttributeName(String attrName) {
    this.addressSuppressionAttributeName = attrName;
  }

  public String getEmailSuppressionAttributeName() {
    return this.emailSuppressionAttributeName;
  }

  public void setEmailSuppressionAttributeName(String attrName) {
    this.emailSuppressionAttributeName = attrName;
  }

  public String getNameSuppressionAttributeName() {
    return this.nameSuppressionAttributeName;
  }

  public void setNameSuppressionAttributeName(String attrName) {
    this.nameSuppressionAttributeName = attrName;
  }

  public String getPersonalSuppressionAttributeName() {
    return this.personalSuppressionAttributeName;
  }

  public void setPersonalSuppressionAttributeName(String attrName) {
    this.personalSuppressionAttributeName = attrName;
  }

  public String getPhoneSuppressionAttributeName() {
    return this.phoneSuppressionAttributeName;
  }

  public void setPhoneSuppressionAttributeName(String attrName) {
    this.phoneSuppressionAttributeName = attrName;
  }

  private void validateConfiguration() {
    if (addressSuppressionAttributeName == null) {
      String msg = "The property 'addressSuppressionAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (emailSuppressionAttributeName == null) {
      String msg = "The property 'emailSuppressionAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (nameSuppressionAttributeName == null) {
      String msg = "The property 'nameSuppressionAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (personalSuppressionAttributeName == null) {
      String msg = "The property 'personalSuppressionAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (phoneSuppressionAttributeName == null) {
      String msg = "The property 'phoneSuppressionAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
  }
//end: helper methods

}
